﻿using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;
using LiftHub.Controls;
using LiftHub.Data.TodoModels;
using LiftHub.ViewModels.Pages;

namespace LiftHub.Pages;

public partial class CalendarPage : PageView
{
    public CalendarPage()
    {
        InitializeComponent();
    }

    private void DateTextInitialized(object? sender, EventArgs e)
    {
        if(sender is not TextBlock block)return;
        block.Text = DateTime.Today.ToString("yyyy-M-d dddd",new CultureInfo("zh-CN", false));
    }

    private async void CreateTodoClick(object? sender, RoutedEventArgs e)
    {
        await AddTodoDialog.ShowAsync();
    }

    private void RemoveStepClick(object? sender, RoutedEventArgs e)
    {
        if(sender is not Control control)return;
        if(control.DataContext is not StepModel step)return;
        if(DataContext is not CalendarPageModel model)return;
        model.Remove(step);
    }

    private void TodoDialogDone(TaskDialogButton sender, EventArgs args)
    {
        if(AddTodoDialog.Content is not AddTodoControl content)return;
        var result =  content.Done();
        if(result == null)return;
        if(DataContext is not CalendarPageModel model)return;
        model.Add(result);
    }

    private void TodoDeleteClick(object? sender, RoutedEventArgs e)
    {
        if(sender is not Control control)return;
        if(control.DataContext is not ToDoModel todo)return;
        if(DataContext is not CalendarPageModel model)return;
        model.Remove(todo);
    }

    public override void PageClosing()
    {
        
    }

    private async void CreateStepClick(object? sender, RoutedEventArgs e)
    {
        await AddStepDialog.ShowAsync();
    }

    private void StepDialogDone(TaskDialogButton sender, EventArgs args)
    {
        if(AddStepDialog.Content is not AddStepControl content)return;
        var result =  content.Done();
        if(result == null)return;
        if(DataContext is not CalendarPageModel model)return;
        model.Add(result);
    }
}